<?php
/**
 * User: 魔十七
 * Date: 2020/11/11
 * Time: 8:30
 */

namespace Samecity\Controller;

use Think\Controller;
use Samecity\Model\CateringAuditModel;
use Samecity\Model\CateringModel;

class CateringAuditController extends Controller
{
    private $CateringAuditModel;
    private $CateringModel;

    protected function _initialize()
    {
        $this->CateringAuditModel = new \Samecity\Model\CateringAuditModel();
        $this->CateringModel = new \Samecity\Model\CateringModel();
    }

    /**
     * 检测当前用户是否审核
     *
     */
    public function isAuditForm()
    {
        $post = $_POST;
        $uid  = isset($post['uid']) ? $post['uid'] : $this->ajaxReturn(array('code'=>1002,'msg'=>'uid异常')); /* user_id */
        $Audit = $this->CateringAuditModel->getAuditUid($uid); /* 用户是否提交过审核 */
        if ($Audit){/* 已经提交过 */
            if ($Audit['audit_type'] == 0){   /* audit_type=0   商铺正在审核中 */
                $result['code']  = 0;
                $result['msg']   = '正在审核中!';
                $result['data']   = $Audit['bz'];
            }
            if ($Audit['audit_type'] == 1){   /* audit_type=1   商铺审核成功,返回数据 */
                $AuditSucceed = $this->CateringModel->CateringAdd($Audit);
                $result['code']  = 1001;
                $result['msg']   = '审核通过!';
                $result['bz']   = '请牢记您的账号密码,切勿告诉其他人哟!';
                $result['url']  = 'http://'. $_SERVER['HTTP_HOST']. '/index.php/Catering/Login/Login';
                $result['data'] = $AuditSucceed;
            }
            /* 查看审核时间*/
            $numTime = '+' . $Audit['day'] . ' day';
            $time = strtotime($numTime,$Audit['update_time']);
            $time1 = time();

            if ($time >= $time1){/* 时间还没到 */
                $result['code'] =1002;
                $result['msg']  = '抱歉,未到审核时间,不能操作!';
                if ($Audit['audit_type'] == 2){
                    $result['info'] = '审核失败';
                }elseif($Audit['audit_type'] == 3){
                    $result['info'] = '审核驳回';
                }
                $result['bz'] =  $Audit['bz'];
                $result['data'] = array(
                    'hint'  => '于' . date('Y-m-d H:i:s',$time) .'方可再次操作,请耐心等待!',
                );
            } else {
                $result['code']  = 0;
                $result['msg']   = 'succeed!';
                $result['data']  = '可授权页面!';
            }
        }else{
            $result['code']  = 0;
            $result['msg']   = 'succeed!';
            $result['data']  = '可授权页面!';
        }
        return $this->ajaxReturn($result);

    }


    /**
     * 店铺审核
     * @param   $uid                uid                 用户ID
     * @param   $title              titile              店铺名称
     * @param   $businessLicense    business_license    营业执照号
     * @param   $xm                 xm                  法人姓名
     * @param   $tel                tel                 法人电话
     * @param   $type               type                店铺类型
     * @param   $gs_cer             image               图片
     * @param   $province           province            省份
     * @param   $shi                shi                 城市
     * @param   $county             county              地区
     * @param   $city               city                详情地址
     * @param   $longitude          longitude           经度
     * @param   $latitude           latitude            维度
     */
    public function submit()
    {
        $post = $_POST;
        $uid  = isset($post['uid']) ? $post['uid'] : $this->ajaxReturn(array('code'=>1002,'msg'=>'uid异常')); /* user_id */
        $Audit = $this->CateringAuditModel->getAuditUid($uid);/* 判断是否提交过 */
        if ($Audit){
            if ($Audit['audit_type'] == 0){   /* audit_type=0   商铺正在审核中 */
                $result['code']  = 0;
                $result['msg']   = '正在审核中!';
                $result['data']   = $Audit['bz'];
            }
            if ($Audit['audit_type'] == 1){   /* audit_type=1   商铺审核成功,返回数据 */
                $AuditSucceed = $this->CateringModel->CateringAdd($Audit);
                $result['code']  = 1001;
                $result['msg']   = '审核通过!';
                $result['bz']   = '请牢记您的账号密码,切勿告诉其他人哟!';
                $result['url']  = 'http://'. $_SERVER['HTTP_HOST']. '/index.php/Catering/Login/Login';
                $result['data'] = $AuditSucceed;
            }
            /* 查看审核时间*/
            $numTime = '+' . $Audit['day'] . ' day';
            $time = strtotime($numTime,$Audit['update_time']);
            $time1 = time();

            $AuditArr = $this->getData($uid,$post);
            if ($time >= $time1){/* 时间还没到 */
                $result['code'] =1002;
                $result['msg']  = '抱歉,未到审核时间,不能操作!';
                if ($Audit['audit_type'] == 2){
                    $result['info'] = '审核失败';
                }elseif($Audit['audit_type'] == 3){
                    $result['info'] = '审核驳回';
                }
                $result['bz'] =  $Audit['bz'];
                $result['data'] = array(
                    'hint'  => '于' . date('Y-m-d H:i:s',$time) .'方可再次操作,请耐心等待!',
                );
            } else {/* 可以重新提交了 */
                if ($Audit['audit_type'] == 2 ){/* 店铺审核失败时 */
                    $data = $this->getAnewSubmit($AuditArr,$Audit['audit_type'],$Audit['uid']);// 重新发起审核操作
                }
                if ($Audit['audit_type'] == 3){/* 店铺审核驳回时 */
                    $data = $this->getAnewSubmit($AuditArr,$Audit['audit_type'],$Audit['uid']);// 重新发起审核操作
                }
                if ($data){
                    $result['code'] = 1001;
                    $result['msg']  = '审核再次提交,请耐心等待!';
                    $result['data'] = $data;
                }
            }

        } else {
            /* 可以直接提交 */
            $AuditArr = $this->getData($uid,$post);
            $data = $this->CateringAuditModel->getAuditAdd($AuditArr);
            $result['data'] = $data;
            if ($data){
                $result['code'] = 1001;
                $result['msg']  = '审核已提交,请耐心等待!';
            }else{
                $result['code'] =1002;
                $result['msg']  = '系统操作繁忙,请稍后重试!';
            }
        }

        return $this->ajaxReturn($result);
    }



    /**
     * 店铺审核(废弃)
     * @param   $uid                uid                 用户ID
     * @param   $title              titile              店铺名称
     * @param   $businessLicense    business_license    营业执照号
     * @param   $xm                 xm                  法人姓名
     * @param   $tel                tel                 法人电话
     * @param   $type               type                店铺类型
     * @param   $gs_cer             image               图片
     * @param   $province           province            省份
     * @param   $shi                shi                 城市
     * @param   $county             county              地区
     * @param   $city               city                详情地址
     * @param   $longitude          longitude           经度
     * @param   $latitude           latitude            维度
     */
    public function CateringAuditSubmit()
    {
        $post = $_POST;
        $uid  = isset($post['uid']) ? $post['uid'] : $this->ajaxReturn(array('code'=>1002,'msg'=>'uid异常'));

        /* 当前用户是否审核过 */
        $Audit = $this->CateringAuditModel->getAuditUid($uid);

        if (!$Audit){
            /* 没有审核过的 */
            $AuditArr = $this->getData($uid,$post);

            $data = $this->CateringAuditModel->getAuditAdd($AuditArr);
            $result['data'] = $data;
            if ($data){
                $result['code'] = 1001;
                $result['msg']  = '审核已提交,请耐心等待!';
            }else{
                $result['code'] =1002;
                $result['msg']  = '系统操作繁忙,请稍后重试!';
            }
        } else {
            if ($Audit['audit_type'] == 1){
                /* 审核通过,返回店铺信息 */
                $AuditSucceed = $this->CateringModel->CateringAdd($Audit);
                $result['code']  = 1001;
                $result['msg']   = '审核通过!';
                $result['bz']   = '请牢记您的账号密码,切勿告诉其他人哟!';
                $result['url']  = 'http://'. $_SERVER['HTTP_HOST']. '/index.php/Catering/Login/Login';
                $result['data'] = $AuditSucceed;
            } elseif($Audit['audit_type'] == 3) {
                /* 审核驳回, 返回驳回原因 */
                $result['code']  = 1003;
                $result['msg']   = '审核驳回!';
                $result['data'] = $Audit['bz'] . ',请' . $Audit['day'] . '天重新审核!';
            } elseif($Audit['audit_type'] == 2) {
                /* 审核失败, 返回失败原因 */
                $result['code']  = 1002;
                $result['msg']   = '审核失败!';
                $result['data'] = $Audit['bz'] . ',请' . $Audit['day'] . '天重新审核!';
            } else {
                $result['code']  = 0;
                $result['msg']   = '正在审核中!';
                $result['data']   = $Audit['bz'];
            }
        }
        return $this->ajaxReturn($result);
    }


    /**
     * 二次审核(废弃)
     * (审核失败,审核驳回)
     * @param   $uid    用户ID
     */
    public function AuditTwoSubmit()
    {
        $post = $_POST;
        $uid  = isset($post['uid']) ? $post['uid'] : $this->ajaxReturn(array('code'=>1002,'msg'=>'uid异常'));
        $Audit = $this->CateringAuditModel->getAuditUid($uid);

        /* 发起审核时间 strtotime()*/
        $numTime = '+' . $Audit['day'] . ' day';
        $time = strtotime($numTime,$Audit['update_time']);
        $AuditArr = $this->getData($uid,$post);

        if (time() >= $time){
            /* 店铺审核失败时 */
            if ($Audit['audit_type'] == 2 ){
                // 重新发起审核操作
                $data = $this->getAnewSubmit($AuditArr,$Audit['audit_type'],$Audit['uid']);
            }
            /* 店铺审核驳回时 */
            if ($Audit['audit_type'] == 3){
                // 重新发起审核操作
                $data = $this->getAnewSubmit($AuditArr,$Audit['audit_type'],$Audit['uid']);
            }
            if ($data){
                $result['code'] = 1001;
                $result['msg']  = '审核再次提交,请耐心等待!';
                $result['data'] = $data;
            }else{
                $result['code'] =1002;
                $result['msg']  = '系统操作繁忙,请稍后重试!';
                $result['data'] = $data;
            }

        } else {
            /* 返回剩余时间 */
            $result['code'] =1002;
            $result['msg']  = '抱歉,未到审核时间,不能操作!';
            $result['data'] = array(
                'hint'  => '于' . date('Y-m-d H:i:s',$time) .'方可再次操作,请耐心等待!',
            );
        }
        return $this->ajaxReturn($result);
    }



    /**
     * 需提交的数据
     */
    protected function getData($uid,$post)
    {
        $title              = isset($post['title']) ? $post['title'] : $this->ajaxReturn(array('code'=>1002,'msg'=>'店铺名称为空'));
        $businessLicense    = isset($post['business_license']) ? $post['business_license'] : $this->ajaxReturn(array('code'=>1002,'msg'=>'营业执照号为空'));
        $xm                 = isset($post['xm']) ? $post['xm'] : $this->ajaxReturn(array('code'=>1002,'msg'=>'法人姓名为空'));
        $tel                = isset($post['tel']) ? $post['tel'] : $this->ajaxReturn(array('code'=>1002,'msg'=>'法人电话为空'));
        $type               = isset($post['type']) ? $post['type'] : $this->ajaxReturn(array('code'=>1002,'msg'=>'请选择类型'));
        $gs_cer             = isset($post['image'])  ?  $post['image'] : '';
        $province           = isset($post['province']) ? $post['province'] : $this->ajaxReturn(array('code'=>1002,'msg'=>'请选择省份'));
        $shi                = isset($post['shi']) ? $post['shi'] : $this->ajaxReturn(array('code'=>1002,'msg'=>'请选择城市'));
        $county             = isset($post['county']) ? $post['county'] : $this->ajaxReturn(array('code'=>1002,'msg'=>'请选择地区'));
        $city               = isset($post['city']) ? $post['city'] : $this->ajaxReturn(array('code'=>1002,'msg'=>'请填写详情地址'));
        $longitude          = isset($post['longitude']) ? $post['longitude'] : '';
        $latitude           = isset($post['latitude']) ? $post['latitude'] : '';

        $AuditArr = array(
            'uid'                   => $uid,
            'title'                 => $title,
            'business_license'      => $businessLicense,
            'xm'                    => $xm,
            'tel'                   => $tel,
            'type'                  => $type,
            'gs_cer'                => $gs_cer,
            'province'              => $province,
            'shi'                   => $shi,
            'county'                => $county,
            'city'                  => $city,
            'longitude'             => $longitude,
            'latitude'              => $latitude
        );
        return $AuditArr;
    }

    /**
     * 重新发布审核
     */
    public function getAnewSubmit($AuditArr,$AuditType)
    {
        $data = $this->CateringAuditModel->getAuditAdd($AuditArr,$AuditType);
        return $data;
    }
}